package com.ruoyi.web.controller.greatWall;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateUtil;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.file.ImageUtils;
import com.ruoyi.framework.config.ExceptionThrower;
import com.ruoyi.framework.enums.ErrorInfo;
import com.ruoyi.system.domain.greatWall.GwScenicInfo;
import com.ruoyi.system.service.GwInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zxy
 * @date 2022/2/11 16:42
 */
@RestController
@RequestMapping(value = "/gwInfo")
public class GwInfoController extends BaseController {

    @Autowired
    private GwInfoService gwInfoService;


    @PutMapping
    public GwScenicInfo save(@RequestBody GwScenicInfo info){
        QueryWrapper<GwScenicInfo> wrapper=new QueryWrapper<>();

        //判断部门是否存在
        LoginUser user = SecurityUtils.getCurrentUser();
        if (StringUtils.isEmpty(info.getDepId())){
            info.setDepId(String.valueOf(user.getDeptId()));
            wrapper.eq("dep_id",user.getDeptId());
        }else {
            wrapper.eq("dep_id",info.getDepId());
        }
        List<Object> list;
        wrapper.eq("title",info.getTitle());
        wrapper.eq("cate_id",info.getCateId());
        if (StringUtils.isEmpty(info.getCreateDate())){
            info.setCreateDate(DateUtil.getNowTime());
        }
        if (info.getStatus() ==null){
            info.setStatus(0);
        }

        info.setDepId(String.valueOf(SecurityUtils.getCurrentUser().getDeptId()));

        if (StringUtils.isNotEmpty(info.getId())){
            wrapper.ne("id",info.getId());
             list= gwInfoService.listObjs(wrapper);
        }else {
            list=gwInfoService.listObjs(wrapper);
        }

        boolean flag = list.size() ==0;
        if (flag){
            gwInfoService.saveOrUpdate(info);
        }else {
            ExceptionThrower.of(ErrorInfo.TITLE_EXIST);
        }

        return info;
    }


    @DeleteMapping(value = "/{id}")
    public void delete(@PathVariable String id){
        gwInfoService.removeById(id);
    }

    @GetMapping(value = "/{id}")
    public AjaxResult getById(@PathVariable String id){
        GwScenicInfo info = gwInfoService.getById(id);
        if (SecurityUtils.isAdminNull()){
            String content="";
            if (StringUtils.isNotEmpty(info.getContent())){
                 content = ImageUtils.getContent(info.getContent());
            }
            info.setContent(content);
        }
        return success(info);
    }


    @GetMapping(value = "/list")
    public TableDataInfo page( GwScenicInfo info){
        startPage();
        return getDataTable(gwInfoService.pageSearch(info));
    }

    @GetMapping(value = "/review/{id}/{status}")
    public void status(@PathVariable String id,@PathVariable Integer status){
        gwInfoService.setStatus(id,status);

    }

}
